<template>
  <div class="user-wrapper">
    <div class="top">
      <Input ref="searchInput" placeholder="请输入手机号或昵称" class="input-cls" @search="handleSearch" />
      <el-select v-model="identityType" placeholder="请选择" clearable @change="handleSelectChange">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </div>
    <div class="bottom">
      <el-table :data="list" style="width: 100%" :height="height">
        <el-table-column label="用户" width="180">
          <template slot-scope="scope">
            <div>
              <el-avatar :size="60" :src="scope.row.portrait_url" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="昵称" width="180" />
        <el-table-column prop="phone" label="手机号" width="150" />
        <el-table-column label="身份">
          <template slot-scope="scope">
            <div>{{ scope.row.identity_type ? '设计师' : '游客' }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="注册时间" width="150" />
        <el-table-column prop="address" label="操作" fixed="right"  width="250">
          <template slot-scope="scope">
            <div>
              <span
                v-if="!scope.row.disable"
                class="col-blue"
                @click="changeUserStatusClick(scope)"
              >禁用</span>
              <span
                v-if="scope.row.disable"
                class="col-red"
                @click="changeUserStatusClick(scope)"
              >已禁用</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Pagination
      class="pagination"
      :total="total"
      :cur-page="currentPage"
      @nextPage="handleNextPage"
    />
  </div>
</template>
<script>
import Input from '@/common/input'
import Pagination from '@/common/pagination'
import Resize from '@/common/mixins/resize'
import { getComstomerList, changeUserStatus } from '@/api/http/user'
const ERR_OK = 200
export default {
  components: {
    Input,
    Pagination
  },
  mixins: [Resize],
  data() {
    return {
      height: 0,
      options: [
        {
          value: 0,
          label: '游客'
        },
        {
          value: 1,
          label: '设计师'
        }
      ],
      identityType: '',
      value: [],
      list: [],
      defaultHeight: 280,
      query: '',
      total: 0,
      currentPage: 1
    }
  },
  created() {
    this._getComstomerList({ page: this.currentPage })
  },
  methods: {
    handleNextPage(page) {
      this.currentPage = page
      this._getComstomerList({
        page: this.currentPage,
        identity_type: this.identityType,
        keywords: this.query
      })
    },
    handleSearch(query) {
      this.currentPage = 1
      this.query = query
      this._getComstomerList({
        identity_type: this.identityType,
        keywords: this.query,
        page: this.currentPage
      })
    },
    handleSelectChange() {
      this.currentPage = 1
      this._getComstomerList({
        identity_type: this.identityType,
        keywords: this.query,
        page: this.currentPage
      })
    },
    changeUserStatusClick(scope) {
      this._changeUserStatus(scope.row.id)
        .then(res => {
          this._getComstomerList({ page: this.currentPage })
        })
    },
    handleChange() {},
    _getComstomerList({ identity_type, keywords, page }) {
      getComstomerList({ identity_type, keywords, page })
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.list = res.data.data
            this.total = res.data.total
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    _changeUserStatus(id) {
      return changeUserStatus(id)
        .then(res => {
          if (res.code === ERR_OK) {
            console.log(res)
            this.$message({
              message: '操作成功',
              showClose: true,
              type: 'success'
            })
            return Promise.resolve(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/el-table.scss";
@import "@/styles/color.scss";
@import "@/styles/el-pagination.scss";
.user-wrapper {
  .top {
    display: flex;
    align-items: center;
    .input-cls {
      margin-right: 21px;
    }
  }
  .bottom {
    margin-top: 30px;
  }
}
</style>
